//
//  SHStautsCell.swift
//  SHWeibo3
//
//  Created by teacher on 16/7/23.
//  Copyright © 2016年 teacher. All rights reserved.
//

import UIKit
import SnapKit
// cell中使用的间距
let statusCellMargin: CGFloat = 10

class SHStautsCell: UITableViewCell {
    // 记录底部视图的顶部约束
    var toolBarBottomConstraint:Constraint?
    // 定义yigeViewModel
    var statusViewModel: SHStatusViewModel? {
        didSet{
            // 给原创微博赋值
            originalView.statusViewModel = statusViewModel
            /*
                - 首先要先卸载底部视图的顶部约束
                - 如果retweeted_status == nil 代表没有转发微博
                    - 底部视图的top ==  原创微博的bottom
                    - 隐藏转发微博
                - 反之 就是有转发微博
                    - 给转发微博的ViewModel 赋值
                    - 底部视图的top ==  转发微博的bottom
                    - 显示转发微博
            */
            self.toolBarBottomConstraint?.uninstall()
            // 没有转发微博
            if statusViewModel?.status?.retweeted_status == nil {
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                // 隐藏
                retweetView.hidden = true
                
            }else {
                // 记住先赋值
                retweetView.statusViewModel = statusViewModel
                // 有转发微博
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示
                retweetView.hidden = false
            }
            
            // 给底部视图赋值
            toolBarView.statusViewModel = statusViewModel

        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        // 01- 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        // 02- 添加约束
        // 01
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            // 当前约束已经在当前原创微博中约束
            //make.height.equalTo(50)
        }
        // 02
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            // 此约束已经在控件中约束了
           // make.height.equalTo(50)
        }
        
        // 03
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarBottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        // 约束contentView
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)
        }
    }
    
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalView: SHStatusOriginalView = SHStatusOriginalView()
    // 转发微博
    private lazy var retweetView: SHStatusRetweetView = SHStatusRetweetView()
    // 底部视图
    private lazy var toolBarView: SHStatusToolBarView = SHStatusToolBarView()
    
    
}

